package com.zjj.zonghe.test1;

import org.omg.CosNaming.IstringHelper;

import java.util.Random;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int a = 0;
        int b = 0;
        int c = 0;

        System.out.println("--------------欢迎进入羊村游戏世界--------------");
        System.out.println("*********************");
        System.out.println("**    游戏开始    **");
        System.out.println("*********************");

        System.out.println("出拳规则：1.石头 2.剪子 3.布");
        System.out.print("请选择你的角色(1.沸羊羊 2.暖羊羊 3.美羊羊): ");
        String playerRole = scanner.next();
        String playerRoleName = roleName1(playerRole);
        System.out.print("请选择对手角色(1.喜羊羊 2.慢羊羊 3.懒羊羊): ");
        String opponentRole = scanner.next();
        String opponentRoleName = roleName2(opponentRole);

        String startGame ;
        do {
            System.out.print("开始游戏吗？(y/n)");
            startGame = scanner.next();
            if ("n".equalsIgnoreCase(startGame)) {
                System.out.println("游戏结束。");
                return;
            } else if (!"y".equalsIgnoreCase(startGame)) {
                System.out.println("输入错误，请重新选择");
            }
        } while (!"y".equalsIgnoreCase(startGame));

        System.out.println("游戏开始！");

        while (true) {
            System.out.print("请出拳：1.石头 2.剪子 3.布");
            int playerChoice = scanner.nextInt();
            int opponentChoice = random.nextInt(3) + 1;

            String result = playGame(playerChoice, opponentChoice);
            System.out.println("你出拳：" + choiceName(playerChoice));
            System.out.println(opponentRoleName + "出拳：" + choiceName(opponentChoice));
            System.out.println("结果：" + result);

            if ("你赢了！".equals(result)) {
                a++;
            } else if ("你输了！".equals(result)) {
                b++;
            } else if ("平局，加油啊！".equals(result)) {
                c++;
            }

            String nextRound ;
            do {
                System.out.print("是否开始下一轮？(y/n)");
                nextRound = scanner.next();
                if ("n".equalsIgnoreCase(nextRound)) {
                    System.out.println("游戏结束，退出程序");
                    System.out.println("-----------------------------");
                    System.out.println(playerRoleName + " 对决 " + opponentRoleName);
                    System.out.println("次数："+(a+b+c));
                    System.out.println("平局："+c);
                    System.out.println(playerRoleName + "得："+a);
                    System.out.println(opponentRoleName + "得："+b);
                    System.out.println("");
                    if (a > b) {
                        System.out.println("最终结果：" + playerRoleName + "赢！");
                    }
                    if (a < b) {
                        System.out.println("最终结果：" + opponentRoleName + "赢！");
                    } else {
                        System.out.println("双方平手");
                    }
                    System.out.println("-----------------------------");
                    return;
                } else if (!"y".equalsIgnoreCase(nextRound)) {
                    System.out.println("输入错误，请重新选择");
                }
            } while (!"y".equalsIgnoreCase(nextRound));
        }
    }

    public static String playGame(int playerChoice, int opponentChoice) {
        if (playerChoice == opponentChoice) {
            return "平局，加油啊！";
        } else if ((playerChoice == 1 && opponentChoice == 2) ||
                (playerChoice == 2 && opponentChoice == 3) ||
                (playerChoice == 3 && opponentChoice == 1)) {
            return "你赢了！";
        } else {
            return "你输了！";
        }
    }

    public static String choiceName(int choice) {
        switch (choice) {
            case 1:
                return "石头";
            case 2:
                return "剪子";
            case 3:
                return "布";
            default:
                Scanner scanner = new Scanner(System.in);
                System.out.println("输入错误，请重新选择");
                choice = scanner.nextInt();
                return choiceName(choice);
        }
    }

    public static String roleName1(String roleIndex) {
        String[] playerRoles = {"沸羊羊", "暖羊羊", "美羊羊"};
            switch (roleIndex) {
                case "1":
                    return playerRoles[0];
                case "2":
                    return playerRoles[1];
                case "3":
                    return playerRoles[2];
                default:
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("输入错误，请重新选择");
                    String newRoleIndex = scanner.next();
                    return roleName1(newRoleIndex);
            }

    }
    public static String roleName2(String roleIndex) {
        String[] opponentRoles = {"喜羊羊", "慢羊羊", "懒羊羊"};
        switch (roleIndex) {
            case "1":
                return opponentRoles[0];
            case "2":
                return opponentRoles[1];
            case "3":
                return opponentRoles[2];
            default:
                Scanner scanner = new Scanner(System.in);
                System.out.println("输入错误，请重新选择");
                String newRoleIndex = scanner.next();
                return roleName2(newRoleIndex);
        }
    }
}